<template>
  <div class='container-subjects'>
    <el-card>
      <!-- 新增及搜索 -->
      <el-row>
        <el-col :span="22">
          <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item label="学科名称">
              <el-input v-model="formInline.subjectName" placeholder="请输入学科名称"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="clear">清除</el-button>
              <el-button type="primary" @click="onSubmit">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="2">
          <el-button type="success" icon="el-icon-edit" @click="subjectsAdd">新增学科</el-button>
        </el-col>
      </el-row>
      <!-- 信息总数 -->
      <el-alert
        :title= "`数据一共${total}条`"
        type="info"
        :closable="false">
      </el-alert>
      <!-- 表格 -->
      <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
        label="序号"
        type="index"
        :index="indexMethod">
      </el-table-column>
      <el-table-column
      prop="subjectName"
        label="学科名称">
      </el-table-column>
      <el-table-column
      prop="username"
        label="创建者">
      </el-table-column>
      <el-table-column
      prop="addDate"
        label="创建日期">
        <template #default = "obj">
          {{obj.row.addDate | parseTimeByString}}
        </template>
      </el-table-column>
      <el-table-column
      prop="isFrontDisplay"
        label="前台是否显示">
        <template #default="obj">{{obj.row.isFrontDisplay===1?'是':'否'}}</template>
      </el-table-column>
      <el-table-column
      prop="twoLevelDirectory"
        label="二级目录">
      </el-table-column>
      <el-table-column
      prop="tags"
        label="标签">
      </el-table-column>
      <el-table-column
      prop="totals"
        label="题目数量">
      </el-table-column>
      <el-table-column
        label="操作"
        width="250px">
        <template #default="{row}">
          <el-link type="primary" @click="subjectDirectory(row)">学科分类</el-link>&nbsp;&nbsp;
          <el-link type="primary" @click="subjectTags(row)">学科标签</el-link>&nbsp;&nbsp;
          <el-link type="primary" @click="edit(row.id)">修改</el-link>&nbsp;&nbsp;
          <el-link type="primary" @click="del(row.id)">删除</el-link>&nbsp;&nbsp;
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页功能 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="100"
      background
      layout="sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
    <!-- 添加和修改学科 -->
    <subjects-add :form="form" @reload="getSubjectsList" :tableData="tableData" :dialogFormVisible.sync="dialogFormVisible" ref="subjectsAddRef"></subjects-add>
  </div>
</template>

<script>
import subjectsAdd from '../components/subjects-add.vue'
import { list, remove, detail } from '@/api/hmmm/subjects.js'
export default {
  data () {
    return {
      formInline: {
        subjectName: '',
        page: 1,
        pagesize: 10
      },
      total: 0,
      tableData: [],
      dialogFormVisible: false,
      form: {}
    }
  },
  components: { subjectsAdd },
  created () {
    this.getSubjectsList()
  },
  methods: {
    // 获取数据并渲染
    async getSubjectsList () {
      const { data } = await list(this.formInline)
      console.log(data.items)
      this.tableData = data.items
      this.total = data.counts
    },
    // 搜索
    onSubmit () {
      console.log('submit!')
      this.getSubjectsList()
    },
    handleSizeChange (val) {
      this.formInline.pagesize = val
      this.getSubjectsList()
    },
    handleCurrentChange (val) {
      this.formInline.page = val
      this.getSubjectsList()
    },
    clear () {
      this.formInline = {
        subjectName: '',
        page: 1,
        pagesize: 10
      }
      this.getSubjectsList()
    },
    indexMethod (index) {
      return (index + 1) + (this.formInline.page - 1) * this.formInline.pagesize
    },
    subjectsAdd () {
      this.dialogFormVisible = true
      this.$refs.subjectsAddRef.title = '新增学科'
    },
    del (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await remove({ id })
        console.log(res)
        this.$message.success('删除成功')
        this.getSubjectsList()
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    async edit (id) {
      console.log(id)
      this.dialogFormVisible = true
      this.$refs.subjectsAddRef.title = '修改学科'
      const { data } = await detail({ id })
      console.log(data)
      this.form = data
    },
    subjectDirectory (row) {
      console.log(this.$route)
      this.$router.push({ path: `/subjects/directorys?id=${row.id}&name=${row.subjectName}`, query: { name: '学科分类' } })
    },
    subjectTags (row) {
      this.$router.push(`/subjects/tags?id=${row.id}&name=${row.subjectName}`)
    }
  }
}
</script>

<style scoped lang='scss'>
.container-subjects {
  margin: 20px;
}
</style>
